package com.example.demo.service.impl;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.LinkedList;

import com.example.demo.service.UserBehaviorAnalysis;
import com.example.demo.repository.AutoMessageHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.normalClasses.eye.dateToStamp;
import com.normalClasses.eye.logReader;
import com.normalClasses.eye.userBehavior;

@Repository
public class UserBehaviorService implements UserBehaviorAnalysis {
  @Autowired
  private AutoMessageHandler messageHandler;
  @Override
  public  LinkedList<userBehavior> usernameBehavior1(String username,String logName,String[] log) throws FileNotFoundException {
    logReader reader=new logReader(logName);
    LinkedList<userBehavior> list= new LinkedList<userBehavior>();
    //String[] log=null;
    //log=reader.getAllLog();
    for(int i=0;i<log.length;i++){
      String information = reader.getDetailedInformation(log[i]);
      String date = reader.getLogDate(log[i])+" "+reader.getLogTime(log[i]);
      if(information.contains(username)){
        System.out.println(information);
        userBehavior behavior=null;
        try{
          behavior = new userBehavior(dateToStamp.dateTStamp(date),information,1.0);
        }
        catch (java.text.ParseException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        list.add(behavior);
      }
      }
    return list;
 }

  @Override
  public LinkedList<userBehavior> usernameBehavior2(String username, String logName,String[] log,String[] id) throws FileNotFoundException {
    logReader reader = new logReader(logName);
    LinkedList<userBehavior> list=new LinkedList<userBehavior>();
    ArrayList<String> id2=new ArrayList<String>();
    for(int i=0;i<id.length;i++){
      if(!(id2.contains(id[i]))) {
        id2.add(id[i]);
      }
    }
    for(int i=0;i<id2.size();i++){
      for(int j=0;j<log.length;j++){
        if(log[j].contains(id2.get(i))){
          String stamp = null;
          try{
            stamp = dateToStamp.dateTStamp(reader.getLogDate(log[j])+" "+reader.getLogTime(log[j]));
          } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          list.add(new userBehavior(stamp,reader.getDetailedInformation(log[j]),0.6));
        }
      }
    }
    return list;
  }

  @Override
  public LinkedList<userBehavior> usernameBehavior3(String username, String logName,String[] log, String[] cookie) throws FileNotFoundException {
    logReader reader= new logReader(logName);
    LinkedList<userBehavior> list=new LinkedList<userBehavior>();
    for(int i=0;i<cookie.length;i++) {
      for(int j=0;j<log.length;j++) {
        if(log[j].contains(cookie[i])) {
          String stamp = null;
          try {
            stamp = dateToStamp.dateTStamp(reader.getLogDate(log[j])+" "+reader.getLogTime(log[j]));
          } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          list.add(new userBehavior(stamp,reader.getDetailedInformation(log[j]),0.3));
        }
      }
    }
    return list;
  }

  @Override
  public LinkedList<userBehavior> usernameBehavior4(String username, String logName)
      throws FileNotFoundException {
    logReader reader = new logReader(logName);
    String[] log = reader.getAllLog();
    LinkedList<userBehavior> list = new LinkedList<userBehavior>();
    String[] id = this.messageHandler.Auto_get_userip(username);
    String[] cookie = this.messageHandler.Auto_get_usercookie(username);
    if(log!=null){
    for(int i=0; i<log.length; i++){
      //System.out.println(log.length+"sss");
      //System.out.println(i+"hhh"+log[i]);
      String information = reader.getDetailedInformation(log[i]);
      String date = reader.getLogDate(log[i])+" "+reader.getLogTime(log[i]);
      if(information.contains(username)){
        //System.out.println(information);
        userBehavior behavior=null;
        try {
          behavior = new userBehavior(dateToStamp.dateTStamp(date),information,1.0);
        } catch (java.text.ParseException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        list.add(behavior);
      }
     }
    ArrayList<String> id2=new ArrayList<String>(); 
    if(id!=null){
    for(int i=0;i<id.length;i++){
      if(!(id2.contains(id[i]))){
        id2.add(id[i]);
      }
    }
    for(int i=0;i<id2.size();i++){
      for(int j=0;j<log.length;j++){
        if(log[j].contains(id2.get(i))){
          String stamp = null;
          try{
            stamp = dateToStamp.dateTStamp(reader.getLogDate(log[j])+" "+reader.getLogTime(log[j]));
          }catch (java.text.ParseException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          list.add(new userBehavior(stamp,reader.getDetailedInformation(log[j]),0.6));
        }
      }
    }
  }
    if (cookie!=null){
    for(int i=0;i<cookie.length;i++){
      for(int j=0;j<log.length;j++){
        if(log[j].contains(cookie[i])){
          String stamp = null;
          try{
            stamp = dateToStamp.dateTStamp(reader.getLogDate(log[j])+" "+reader.getLogTime(log[j]));
          } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          list.add( new userBehavior(stamp, reader.getDetailedInformation(log[j]),0.3));
        }
      }
    }
   }
  }
    return list;
    
  }

}
